package com.tro.common.util.string;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class StringUtlis {
	public static final char UNDERLINE = '_';

	public static Boolean isEmail(String address) {
		String format = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z][@]([a-z0-9]+[-]?){1,}[.]\\p{Lower}{2,}";
		if (address.matches(format)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 驼峰格式字符串转换为下划线格式字符串
	 * 
	 * @param param
	 * @return
	 */
	public static String camelToUnderline(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append(UNDERLINE);
				sb.append(Character.toLowerCase(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 下划线格式字符串转换为驼峰格式字符串
	 * 
	 * @param param
	 * @return
	 */
	public static String underlineToCamel(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		int len = param.length();
		StringBuilder sb = new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c = param.charAt(i);
			if (c == UNDERLINE) {
				if (++i < len) {
					sb.append(Character.toUpperCase(param.charAt(i)));
				}
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * 下划线格式字符串转换为驼峰格式字符串2
	 * 
	 * @param param
	 * @return
	 */
	public static String underlineToCamel2(String param) {
		if (param == null || "".equals(param.trim())) {
			return "";
		}
		StringBuilder sb = new StringBuilder(param);
		Matcher mc = Pattern.compile("_").matcher(param);
		int i = 0;
		while (mc.find()) {
			int position = mc.end() - (i++);
			sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
		}
		return sb.toString();
	}

	public static String getGUID() {
		return getUUID().replace("-", "");
	}

	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	public static boolean sqlValidate(String str) {
		str = str.toLowerCase();// 统一转为小写
		String badStr = "'|and|exec|execute|insert|select|delete|update|count|drop|*|%|chr|mid|master|truncate|"
				+ "char|declare|sitename|net user|xp_cmdshell|;|or|-|+|,|like'|and|exec|execute|insert|create|drop|"
				+ "table|from|grant|use|group_concat|column_name|"
				+ "information_schema.columns|table_schema|union|where|select|delete|update|order|by|count|*|"
				+ "chr|mid|master|truncate|char|declare|or|;|-|--|+|,|like|//|/|%|#";// 过滤掉的sql关键字，可以手动添加
		String[] badStrs = badStr.split("\\|");
		for (int i = 0; i < badStrs.length; i++) {
			if (str.indexOf(badStrs[i]) >= 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 * @Title: cleanHtml
	 * @Description: TODO(转换html后不能直接添加到文本域，去掉 html/body)
	 * @param @param
	 *            html
	 * @param @return
	 *            设定文件
	 * @return String 返回类型
	 * @throws @author
	 *             leeko
	 */
	public static String cleanHtml(String html) {
		html = html.replace("<html>", "");
		html = html.replace("</html>", "");
		html = html.replace("<body>", "");
		html = html.replace("</body>", "");
		return html;
	}

	public static String cleanMailUser(String val) {
		if (StringUtils.isNotBlank(val)) {
			val = val.replace("<", "&lt;");
			val = val.replace(">", "&gt;");
		}
		return val;
	}

	static String[] word = { "doc", "docx" };
	static String[] excel = { "xls", "xlsx", "csv" };
	static String[] txt = { "txt" };
	static String[] ppt = { "ppt", "pptx" };
	static String[] zip = { "zip", "rar", "tar", "7z" };
	static String[] pic = { "png", "jpg", "gif", "jpeg" };

	public static boolean useLoop(String[] arr, String targetValue) {
		for (String s : arr) {
			if (s.equals(targetValue))
				return true;
		}
		return false;
	}

	public static Map<String, Object> getStyle(String suffix) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("style", "icon-standard-page-white");
		result.put("allowPre", 0);
		/*
		 * word:icon-standard-page-word excel:icon-standard-page-white-excel
		 * ppt:icon-standard-page-white-powerpoint
		 * zip:icon-standard-page-white-zip txt：icon-standard-page-white-text
		 * photo:icon-hamburg-graphy 未知格式:icon-standard-page-white
		 */
		suffix = suffix.toLowerCase();
		if (useLoop(word, suffix)) {
			result.put("style", "icon-standard-page-word");
			result.put("allowPre", 1);
			return result;
		}
		if (useLoop(excel, suffix)) {
			result.put("style", "icon-standard-page-white-excel");
			result.put("allowPre", 1);
			return result;
		}
		if (useLoop(ppt, suffix)) {
			result.put("style", "icon-standard-page-white-powerpoint");
			result.put("allowPre", 1);
			return result;
		}
		if (useLoop(pic, suffix)) {
			result.put("style", "icon-hamburg-graphy");
			result.put("allowPre", 1);
			return result;
		}
		if (useLoop(zip, suffix)) {
			result.put("style", "icon-standard-page-white-zip");
			return result;
		}
		if (useLoop(txt, suffix)) {
			result.put("style", "icon-standard-page-white-text");
			result.put("allowPre", 1);
			return result;
		}

		return result;
	}

	public static Map<String, String> fileName(String path) {
		Map<String, String> result = new HashMap<String, String>();

		result.put("path", "");
		int index = path.lastIndexOf("/");
		if (index != -1) {
			String basePath = path.substring(0, index + 1);
			result.put("path", basePath);
		}
		String fileName = path.substring(index + 1, path.length());
		result.put("fileName", fileName);

		return result;
	}

	/**
	 * 
	 * @Title: addressHandler
	 * @Description: TODO(处理admin;admin<admin>;likun<likun>; 移除指定用户)
	 * @param @param
	 *            address
	 * @param @return
	 *            设定文件
	 * @return String 返回类型
	 * @throws @author
	 *             leeko
	 */
	public static String addressHandler(String address, String single) {
		if (StringUtils.isBlank(address)) {
			return "";
		}
		String[] adds = address.split(";");
		String result = "";
		for (int i = 0; i < adds.length; i++) {
			if (adds[i].equals(single)) {
			} else {
				Pattern pattern = Pattern.compile("(?<=\\<)(.+?)(?=\\>)");
				Matcher matcher = pattern.matcher(adds[i]);
				if (matcher.find()) {
					if (matcher.group().equals(single)) {

					} else {
						result = result + adds[i] + ";";
					}
				} else {
					result = result + adds[i] + ";";
				}
			}
		}
		return address;
	}

	public static void main(String[] args) {
		System.out.println(addressHandler(null, "admin"));
		// fileName("/static/picture/20171027151012314.doc");
		/*
		 * String val=
		 * "可视化系统部署文档.doc|/static/picture/20171027151012314.doc|154.5KB|doc,6633D368BB9425C68DE8DEF0CA8C4AD7.png|/static/picture/20171027151012325.png|59.8KB|png,人员系分.html|/static/picture/20171027151012325.html|38.5KB|html";
		 * if (StringUtils.isNotBlank(val)) { String[] annexList =
		 * val.split(","); for (String an : annexList) { System.out.println(an);
		 * String[] m = an.split("\\|"); if (m.length != 4) {
		 * System.out.println(m.length+""); } else { System.out.println(m[0]);
		 * System.out.println(m[1]); System.out.println(m[2]);
		 * System.out.println(m[3]); } } }
		 */
		
		System.out.println(isEmail("likun@tro-s.com"));
	}

}
